#include <bits/stdc++.h>

using namespace std;
const int N = 50010;

int p[N], d[N];
int k, n;
int find(int x)
{
    if (p[x] != x)
    {
        int t = find(p[x]);
        d[x] += d[p[x]];
        p[x] = t;
    }
    return x;
}
int main()
{
    cin >> n >> k;
    for (int i = 1; i <= n; ++i)
    {
        p[i] = i;
    }
    int ret = 0;
    while (k--)
    {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        if (b > n || c > n)
        {
            ret++;
        }
        else
        {
            int pb = find(b), pc = find(c);
            if (a == 1)
            {
                if (pb == pc && (d[b] - d[c]) % 3)
                {
                    ret++;
                }
                else if (pb != pc)
                {
                    p[pb] = pc;
                    d[pb] = d[c] - d[b]; // d[b] + ? - d[c] == 0
                }
            }
            else
            {
                if (pb == pc && (d[b] - d[c] - 1) % 3)
                {
                    ret++;
                }
                else if (pb != pc)
                {
                    p[pb] = pc;
                    d[pb] = d[c] + 1 - d[b]; // d[b] + ? - d[c]  == 1
                }
            }
        }
    }
    printf("%d", ret);
    return 0;
}